package com.game;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;

public class GaismaActivity extends Activity {
	
	
	private GaismaView mGameView;
	private GaismaThread mThread;	

	private static final int MENU_PAUSE = 0;
	private static final int MENU_RESUME = 1;

	
	/* (non-Javadoc)
	 * Creates the in-game options menu that pops up with the Home key
	 * Uses res/menu/principal_menu.xml
	 * 
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 */
	@Override public boolean onCreateOptionsMenu(Menu menu)
	{		
		super.onCreateOptionsMenu(menu);
	        
	    menu.add(0, MENU_PAUSE, 0, R.string.menu_pause);
	    menu.add(0, MENU_RESUME, 0, R.string.menu_resume);
	    return true;		
	}
	
	
	/* (non-Javadoc)
	 * Determines what does each element defined on the onCreateOptionsMenu()
	 * function does.
	 * 
	 * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
	 */
	@Override public boolean onOptionsItemSelected(MenuItem item)
	{
		switch (item.getItemId()) 
		{
	        case MENU_PAUSE:     mThread.pause();   return true;
	        case MENU_RESUME:    mThread.unPause(); return true;      
		}
		return false;
	}
	
	
	/* (non-Javadoc)
	 * Does the main initialization of the application. 
	 * It creates a full screen view, sets the layout (dependant on 
	 * res/layout/play_scene.xml), sets the screen resolution and 
	 * calls either doStart() or resume()for the thread, dependant 
	 * on if there is a save game or not.
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override protected void onCreate(Bundle savedInstance)
	{
		Log.i("GAISMA LOG:: ",this.getClass().getName()+"::onCreate" );
		
		super.onCreate(savedInstance);

		// We create a full-screen window.
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.getWindow().setFlags(	WindowManager.LayoutParams.FLAG_FULLSCREEN,
        							WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		// Creates the layout for the activity.
		setContentView(R.layout.play_scene);
		
		// Create game view (input and application layout) and the main thread (main loop) 
		mGameView  = (GaismaView) findViewById(R.id.gaismaView);
		mThread = new GaismaThread(mGameView.getHolder(), mGameView.getContext());
		
		// Provide the gameView with access to the thread
		mGameView.setmThread(mThread);
		
		// Notify the thread of the actual resolution
		mThread.setScreenResolution(getWindowManager().getDefaultDisplay().getWidth(), 
									getWindowManager().getDefaultDisplay().getHeight());
		
		
		// Load or not "Last game" parameters
		if(savedInstance == null)
		{
			Log.i("GAISMA LOG:: ","No saved instance");
			mThread.doStart();
			
		}
		else
		{
			Log.i("GAISMA LOG:: ","Saved instance found" );
			mThread.restoreState(savedInstance);
		}
		
		
		
	}
	
	
	/* (non-Javadoc)
	 * Pauses the game.
	 * 
	 * @see android.app.Activity#onPause()
	 */
	@Override protected void onPause()
	{
		Log.i("GAISMA LOG:: ",this.getClass().getName()+"::onPause"  );
		super.onPause();
		mThread.pause();
	}
		
	
	/* (non-Javadoc)
	 * Saves the game.
	 * TODO: Does not work
	 * 
	 * @see android.app.Activity#onSaveInstanceState(android.os.Bundle)
	 */
	@Override protected void onSaveInstanceState(Bundle outState)
	{
		Log.i("GAISMA LOG:: ",this.getClass().getName()+"::onSaveInstanceState"  );
		
		super.onSaveInstanceState(outState);
		mThread.saveState(outState);
		
	}
	
}
